--
module("UI.Windows.LoginWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local LoginModule = require("Module.LoginModule").LoginModule
LoginWin = UIWindow:new()

LoginWin.name = "LoginWin"
local json = require("cjson")


function LoginWin:Init()
    local t = self.Container.transform

    self.denglu_Panel = UIUtility.GetChildTransform("denglu_Panel", t, true)
    self.zhuce_Panel = UIUtility.GetChildTransform("zhuce_Panel", t, true)

    self.denglu_denglu_btn = UIUtility.GetChildTransform("denglu_denglu_btn", t, true)
    self.zhuce_zhuce_btn = UIUtility.GetChildTransform("zhuce_zhuce_btn", t, true)

    self.denglu_on_btn = UIUtility.GetChildTransform("denglu_on_btn", t, true)
    self.denglu_off_btn = UIUtility.GetChildTransform("denglu_off_btn", t, true)
    self.zhuce_on_btn = UIUtility.GetChildTransform("zhuce_on_btn", t, true)
    self.zhuce_off_btn = UIUtility.GetChildTransform("zhuce_off_btn", t, true)


    self.denglu_zhaohuimima_btn = UIUtility.GetChildTransform("denglu_zhaohuimima_btn", t, true)

    self.denglu_zhanghao = UIUtility.FindContorl('UIInput', "denglu_zhanghao", t)
    self.denglu_mima = UIUtility.FindContorl('UIInput', "denglu_mima", t)
    self.zhuce_zhanghao = UIUtility.FindContorl('UIInput', "zhuce_zhanghao", t)
    self.zhuce_mima = UIUtility.FindContorl('UIInput', "zhuce_mima", t)
    self.zhuce_querenmima = UIUtility.FindContorl('UIInput', "zhuce_querenmima_lable", t)
    self.zhuce_yaoqingma = UIUtility.FindContorl('UIInput', "zhuce_yaoqingma", t)

    self.zhuce_yzm_lable = UIUtility.FindContorl('UIInput', "zhuce_yzm_lable", t)

    self.LoadingMask = UIUtility.GetChildTransform("LoadingMask", t, true)
    -- --版本
    self.VersionLabel = UIUtility.FindContorl('UILabel', "VersionLabel", t)

    self.jiazai = UIUtility.FindContorl('SpineCtrl', "jiazai", t)

    print("设备:" .. UnityEngine.SystemInfo.deviceUniqueIdentifier)

    local hasLast = UnityEngine.PlayerPrefs.HasKey("denglu_zhanghao")
    if hasLast then
        self.denglu_zhanghao.value = UnityEngine.PlayerPrefs.GetString("denglu_zhanghao");
    end

    local hasLastps = UnityEngine.PlayerPrefs.HasKey("denglu_mima")
    if hasLastps then
        self.denglu_mima.value = UnityEngine.PlayerPrefs.GetString("denglu_mima");
    end

    local VersionTool = require("VersionTool").VersionTool
    self.Version = VersionTool.getVresion()

    self:BindEvents()
end

function LoginWin:OnShow()
    self.VersionLabel.text = self.Version
    self.LoadingMask.gameObject:SetActive(false)
end

function LoginWin:OnClose()

end

function LoginWin:BindEvents()
    function GetLoginDataCallBack(jsondata)
        if nil == jsondata then
            self.LoadingMask.gameObject:SetActive(false)
            return
        end
        self.LoadingMask.gameObject:SetActive(false)
        UIWinMgr:CloseWindow("LoginWin")
        local NewGameLoobyWin = UIWinMgr:OpenWindow("NewGameLoobyWin")
        -- NewGameLoobyWin:ToClubPage()
    end

    self.GetLoginDataCallBack = GetLoginDataCallBack

    function OnLoginBtnClick()
        if not self.denglu_zhanghao.value or "" == self.denglu_zhanghao.value then
            return
        end

        UnityEngine.PlayerPrefs.SetString("denglu_zhanghao", self.denglu_zhanghao.value);

        if not self.denglu_mima.value or "" == self.denglu_mima.value then
            return
        end

        UnityEngine.PlayerPrefs.SetString("denglu_mima", self.denglu_mima.value);

        self.LoadingMask.gameObject:SetActive(true)
        self.jiazai:DoAni("animation", true, false)

        LoginModule:GetLoginData(self.denglu_zhanghao.value, self.denglu_mima.value, GetLoginDataCallBack)
    end

    UIHelper.BindUIEvnet("Click", OnLoginBtnClick, self.denglu_denglu_btn.gameObject)



    function OpenZhuce()
        self.zhuce_off_btn.gameObject:SetActive(false)
        self.zhuce_on_btn.gameObject:SetActive(true)
        self.denglu_off_btn.gameObject:SetActive(true)
        self.denglu_on_btn.gameObject:SetActive(false)
        self.denglu_Panel.gameObject:SetActive(false)
        self.zhuce_Panel.gameObject:SetActive(true)
    end

    UIHelper.BindUIEvnet("Click", OpenZhuce, self.zhuce_off_btn.gameObject)

    function OpenDenglu()
        self.zhuce_off_btn.gameObject:SetActive(true)
        self.zhuce_on_btn.gameObject:SetActive(false)
        self.denglu_off_btn.gameObject:SetActive(false)
        self.denglu_on_btn.gameObject:SetActive(true)
        self.denglu_Panel.gameObject:SetActive(true)
        self.zhuce_Panel.gameObject:SetActive(false)
    end

    UIHelper.BindUIEvnet("Click", OpenDenglu, self.denglu_off_btn.gameObject)

    function OpenFind()
        UIWinMgr:OpenWindow("ZhaoHuiPasswordWin", 1)
    end

    UIHelper.BindUIEvnet("Click", OpenFind, self.denglu_zhaohuimima_btn.gameObject)


    UIHelper.BindUIEvnet("Click", function()
        if self.zhuce_zhanghao.value == "" then
            UIWinMgr:OpenTextNotice("请输入手机号")
            return
        end
        --获取验证码
        require("NetWork.NetHttp").SendRequest(
            require("Module.ConfigModule").ConfigModule.SessionUrl ..
            "/byr/sms/sendCode?phoneNumber=" .. self.zhuce_zhanghao.value
            .. "&uuid=" .. UnityEngine.SystemInfo.deviceUniqueIdentifier, function(ok, data)
                if ok and data.code == 200 then
                    print("获取验证码" .. stringify(data))
                    UIWinMgr:OpenTextNotice("获取验证码成功")
                else
                    print("获取验证码失败" .. stringify(data))
                    UIWinMgr:OpenTextNotice("获取验证码失败")
                end
            end)
    end, UIUtility.GetChildTransform("zhuce_shouji_hq_btn", self.Container.transform, true).gameObject)



    --注册
    function Zhuce()
        if self.zhuce_mima.value == "" then
            UIWinMgr:OpenTextNotice("请输入密码")
            return
        end

        if self.zhuce_mima.value ~= self.zhuce_querenmima.value then
            UIWinMgr:OpenTextNotice("两次输入的密码不一致")
            return
        end

        if self.zhuce_zhanghao.value == "" then
            UIWinMgr:OpenTextNotice("请输入手机号")
            return
        end

        if self.zhuce_yzm_lable.value == "" then
            UIWinMgr:OpenTextNotice("请输入真实姓名")
            return
        end



        local ConfigModule = require("Module.ConfigModule").ConfigModule

        local registerJson = string.format(
            '{"username": "%s","name":"%s","password": "%s","parentAgentCode":"%s","phone":"%s"}',
            UIUtility.FindContorl("UIInput", "zhuce_yhm_lable", self.zhuce_Panel.transform).value,
            self.zhuce_yzm_lable.value,
            self.zhuce_mima.value,
            UIUtility.FindContorl("UIInput", "zhuce_yaoqingma_lable", self.zhuce_Panel.transform).value,
            self.zhuce_zhanghao.value

        )


        NetHelper.SendHttpRequestByASyn(ConfigModule.SessionUrl .. "/aut/buyer/register", registerJson,
            function(strdata)
                local jsondata = json.decode(tolua.tolstring(strdata))
                if jsondata.code == 200 then
                    UIWinMgr:OpenTextNotice("注册账号成功")
                    OpenDenglu()
                else
                    UIWinMgr:OpenTextNotice(jsondata.msg)
                end
                Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~" .. stringify(strdata))
            end)
        Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~")
    end

    UIHelper.BindUIEvnet("Click", Zhuce, self.zhuce_zhuce_btn.gameObject)
end

function LoginWin:TryOldSessionLogin()
    local LoginModule = require("Module.LoginModule").LoginModule
    local hasLast = UnityEngine.PlayerPrefs.HasKey("LastPCAccount")
    local hasLastps = UnityEngine.PlayerPrefs.HasKey("LastPCPassWord")
    if hasLast and hasLastps then
        --UIWinMgr:OpenMask("游戏登陆中...")
        self.LoadingMask.gameObject:SetActive(true)
        local lastAccout = UnityEngine.PlayerPrefs.GetString("LastPCAccount")
        local lastps = UnityEngine.PlayerPrefs.GetString("LastPCPassWord")
        LoginModule:GetLoginData(lastAccout, lastps, self.GetLoginDataCallBack)
    end
end
